package com.eorchis.module.interioruser.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.log.LogRecord;
import com.eorchis.log.LogRecordParam;
import com.eorchis.log.LogThread;
import com.eorchis.module.classdept.dao.IClassDeptDao;
import com.eorchis.module.classdept.domain.ClassDeptQueryBean;
import com.eorchis.module.classdept.service.IClassDeptService;
import com.eorchis.module.classdept.ui.commond.ClassDeptQueryCommond;
import com.eorchis.module.classdept.ui.commond.ClassDeptValidCommond;
import com.eorchis.module.classuser.dao.IClassUserDao;
import com.eorchis.module.classuser.domain.ClassUser;
import com.eorchis.module.classuser.domain.QueryClassUserBean;
import com.eorchis.module.classuser.ui.commond.ClassUserQueryCommond;
import com.eorchis.module.classuser.ui.commond.ClassUserValidCommond;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.deptassignedinfo.domain.DeptAssignedInfo;
import com.eorchis.module.deptassignedinfo.service.IDeptAssignedInfoService;
import com.eorchis.module.deptassignedinfo.ui.commond.DeptAssignedInfoQueryCommond;
import com.eorchis.module.deptassignedinfo.ui.commond.DeptAssignedInfoValidCommond;
import com.eorchis.module.exceluitls.domain.excel.ExcelCell;
import com.eorchis.module.exceluitls.domain.excel.ExcelRow;
import com.eorchis.module.exceluitls.domain.excel.ExcelSheet;
import com.eorchis.module.interioruser.dao.IInteriorUserDao;
import com.eorchis.module.interioruser.domain.DeptSignupBean;
import com.eorchis.module.interioruser.domain.InteriorUser;
import com.eorchis.module.interioruser.domain.InteriorUserQueryBean;
import com.eorchis.module.interioruser.domain.SignupBean;
import com.eorchis.module.interioruser.domain.UserInfoBean;
import com.eorchis.module.interioruser.service.IInteriorUserService;
import com.eorchis.module.interioruser.ui.commond.InteriorUserQueryCommond;
import com.eorchis.module.interioruser.ui.commond.InteriorUserValidCommond;
import com.eorchis.module.preauthorizationregistration.dao.IPreAuthorizationRegistrationDao;
import com.eorchis.module.trainingclass.dao.ITrainingClassDao;
import com.eorchis.module.trainingclass.domain.TrainingClass;
import com.eorchis.module.trainingclass.service.ITrainingClassService;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassQueryCommond;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassValidCommond;
import com.eorchis.module.user.domain.User;
import com.eorchis.util.UniteConstants;
import com.eorchis.module.user.service.IUserService;
import com.eorchis.module.user.ui.commond.UserQueryCommond;
import com.eorchis.module.user.ui.commond.UserValidCommond;
import com.eorchis.module.util.Base64Util;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: InteriorUserServiceImpl<br>
 * Description: 内部用户管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Administrator
 * @createDate 2015-01-04
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.interioruser.service.impl.InteriorUserServiceImpl")
public class InteriorUserServiceImpl extends AbstractBaseService implements IInteriorUserService {

	@Resource(name = "com.eorchis.module.interioruser.dao.impl.InteriorUserDaoImpl")
	private IInteriorUserDao interiorUserDao;
	
	@Autowired
	@Qualifier("com.eorchis.module.classuser.dao.impl.ClassUserDaoImpl")
	private IClassUserDao classUserDao;
	
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.service.impl.TrainingClassServiceImpl")
	ITrainingClassService trainingClassService;
	
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.dao.impl.TrainingClassDaoImpl")
	ITrainingClassDao trainingClassDao;
	
//	@Autowired
//	@Qualifier("com.eorchis.module.deptassignedinfo.dao.impl.DeptAssignedInfoDaoImpl")
//	IDeptAssignedInfoDao deptAssignedInfoDao;
	
	@Autowired
	@Qualifier("com.eorchis.module.deptassignedinfo.service.impl.DeptAssignedInfoServiceImpl")
	IDeptAssignedInfoService deptAssignedInfoService;
	
	@Autowired
	@Qualifier("com.eorchis.module.classdept.dao.impl.ClassDeptDaoImpl")
	IClassDeptDao classDeptDao;
	
	//解密 2015/5/5 -GUOL
	@Resource(name = "com.eorchis.module.util.Base64Util")
	private Base64Util base64Util;
	
	// 班级部门关系表接口
	@Autowired
	@Qualifier("com.eorchis.module.classdept.service.impl.ClassDeptServiceImpl")
	private IClassDeptService classDeptService;
	@Autowired
	@Qualifier("com.eorchis.module.user.service.impl.UserServiceImpl")
	private IUserService userService;
		
	@Resource(name = "com.eorchis.module.preauthorizationregistration.dao.impl.PreAuthorizationRegistrationDaoImpl")
	private IPreAuthorizationRegistrationDao preAuthorizationRegistrationDao;
	private static Object o=new Object();
	@Override
	public IDaoSupport getDaoSupport() {
		return interiorUserDao;
	}

	@Override
	@SuppressWarnings("unchecked")
	public InteriorUserValidCommond toCommond(IBaseEntity entity) {
		return new InteriorUserValidCommond((InteriorUser)entity);
	}

	@Override
	public InteriorUserQueryBean preUpdateInteriorUser(
			InteriorUserQueryCommond qCommond) throws Exception {
		return interiorUserDao.preUpdateInteriorUser(qCommond);
	}

	@Override
	public int updateInteriorUser(InteriorUserValidCommond vCommond)
			throws Exception {
		if(interiorUserDao.updateInteriorUser(vCommond)==1){
			return interiorUserDao.updateBaseUser(vCommond);
		}
		return 0;
	}
	
	
	/**
	 * 添加内部用户
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年1月7日
	 */
	public String saveInteriorUser(InteriorUserValidCommond validCommond) throws Exception{
		String msg="";
		//验证用户是否存在
		UserInfoBean userInfo=interiorUserDao.checkUserIsIn(validCommond);
		if(PropertyUtil.objectNotEmpty(userInfo)){
			InteriorUserQueryCommond checkDeptqueryCommond=new InteriorUserQueryCommond();
			checkDeptqueryCommond.setSearchTreepath(userInfo.getTreepath());
		    boolean checkDept=	interiorUserDao.checkUserInSignUpDept(checkDeptqueryCommond);
		    if(checkDept){
			User user=new User();
			user.setUserId(userInfo.getUserId());
			user.setLoginID(userInfo.getLoginID());
			user.setUserName(userInfo.getUserName());
			//改用户是否在内部用户表中 在 这修改  不在则添加
			InteriorUserValidCommond updatevalidCommond=this.find(userInfo.getUserId());
			if(PropertyUtil.objectNotEmpty(updatevalidCommond)){
				InteriorUserValidCommond vCommond=new InteriorUserValidCommond();
				vCommond.setUserid(userInfo.getUserId());
				vCommond.setUserName(user.getUserName());
				interiorUserDao.updateInteriorUser(vCommond);
			//updatevalidCommond.setUserName(user.getUserName());
			}else{
				InteriorUserValidCommond savevalidCommond=new InteriorUserValidCommond();
				savevalidCommond.setUserid(userInfo.getUserId());		
				savevalidCommond.setUser(user);
				savevalidCommond.setUserName(user.getUserName());
				this.save(savevalidCommond);
			}
			//查询用户是否在其他班级报过名
			InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
			InteriorUserQueryCommond checkUserqueryCommond=new InteriorUserQueryCommond();
			checkUserqueryCommond.setSearchUserId(userInfo.getUserId());
			queryCommond.setSearchUserId(userInfo.getUserId());
			//查询班级信息
			TrainingClass trainingClassValidCommond= trainingClassDao.find(TrainingClass.class, validCommond.getClassId());
			if(PropertyUtil.objectNotEmpty(trainingClassValidCommond)){
				boolean checkNum=checkClassUserNum(trainingClassValidCommond,1);
				if(checkNum){				
				queryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());	
				checkUserqueryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());	
				boolean flag=true;
				//如果是普通班 不考虑班级是否在报名范围内
				if(!trainingClassValidCommond.getClassType().getDataCode().equals(TrainingClass.PTB_CLASS_TYPE)){
					//验证用户是否在班级报名范围内
					queryCommond.setSearchClassId(trainingClassValidCommond.getClassId());
					SignupBean singupBean=checkUserIsInSignUP(queryCommond,userInfo);
					//制定报名机构了
					if(singupBean.getFlag()){
						if(PropertyUtil.objectNotEmpty(singupBean.getClassDeptBean())){
							queryCommond.setSearchClassId(trainingClassValidCommond.getClassId());
							queryCommond.setSeachDepId(singupBean.getClassDeptBean().getDepId());			
							DeptSignupBean deptSignBen=this.getDeptSignupNum(queryCommond).get(0);
							//如果 能报人数不大于已报人数
							if(!(singupBean.getClassDeptBean().getLimitNum()>deptSignBen.getSignupNum().intValue())){
								flag=false;
								msg="单位报名人数已满 ";
							}
						}					
					}else{
						flag=false;
						msg="该用户不在报名范围内 ";
					}
				}			
				//在班级报名范围内
				if(flag){
					//验证用户是否进入过其他班级
					ClassUser clUser=interiorUserDao.checkUserInClass(checkUserqueryCommond);
					if(PropertyUtil.objectNotEmpty(clUser)){
						//msg="该用户已经进入过其他班级 ";
						msg=clUser.getUser().getLoginID()+"学员已经报名"+clUser.getTrainingClass().getClassMainName()+",该学员只能报名1个班级。";
					}else{
						//查询分配名额
						String[] depIds=userInfo.getTreepath().split("/");
						
						InteriorUserQueryCommond iuqueryCommond=new InteriorUserQueryCommond();
						//班级类型
						List<String> classTypeList=new ArrayList<String>();
						DeptAssignedInfoQueryCommond deptAssignedInfoQueryCommond=new DeptAssignedInfoQueryCommond();
						deptAssignedInfoQueryCommond.setSearchDepIds(depIds);
						deptAssignedInfoQueryCommond.setSearchYear(Integer.parseInt(trainingClassValidCommond.getClassnameYear()));
						if(trainingClassValidCommond.getClassType().getDataCode().equals(TrainingClass.GFB_CLASS_TYPE)){
							deptAssignedInfoQueryCommond.setSearchClassType(2);//国防班
							classTypeList.add(TrainingClass.GFB_CLASS_TYPE);
						}else{
							deptAssignedInfoQueryCommond.setSearchClassType(1);
							classTypeList.add(TrainingClass.PTB_CLASS_TYPE);
							classTypeList.add(TrainingClass.DXB_CLASS_TYPE);
						}	
						iuqueryCommond.setSearchClassTypeList(classTypeList);
						List<DeptAssignedInfoValidCommond> deptAssList=(List<DeptAssignedInfoValidCommond>)deptAssignedInfoService.findList(deptAssignedInfoQueryCommond);
						if(PropertyUtil.objectNotEmpty(deptAssList)&&deptAssList.size()>0){
							DeptAssignedInfoValidCommond deptAssInfo=deptAssList.get(0);				
							iuqueryCommond.setSeachDepId(deptAssInfo.getDepartment().getDepid());
							iuqueryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());
							List<DeptSignupBean> deptSidnupList=this.getDeptSignupNum(iuqueryCommond);
							if(PropertyUtil.objectNotEmpty(deptSidnupList)){
								DeptSignupBean deptSidnupbean=deptSidnupList.get(0);
								if(deptAssInfo.getRestrictPersonNum()>deptSidnupbean.getSignupNum().intValue()){

									msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.UNIT);
								}else{
									msg="单位年度报名已满 ";
								}
							}else{

								msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.UNIT);
							}
						}else{

							msg="人员所属单位没有参学名额";
						}							
					}		
				}
			}else{
				msg="班级报名人数已满";
			}
			}
		    }else{
		    	msg="该用户不在报名范围内 ";
		    }
		}else{
			msg="该用户不存在 ";
		}
		
		return msg;	
	}
	
	/**
	 * 添加内部用户 个人报名
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年1月13日
	 */
	public String studentSignup(InteriorUserValidCommond validCommond) throws Exception{
		String msg="";
		//验证用户是否存在
		UserInfoBean userInfo=interiorUserDao.checkUserIsIn(validCommond);
		if(PropertyUtil.objectNotEmpty(userInfo)){
			User user=new User();
			user.setUserId(userInfo.getUserId());
			user.setLoginID(userInfo.getLoginID());
			//改用户是否在内部用户表中  不在则添加
			InteriorUserValidCommond updatevalidCommond=this.find(userInfo.getUserId());
			if(!PropertyUtil.objectNotEmpty(updatevalidCommond)){
				InteriorUserValidCommond savevalidCommond=new InteriorUserValidCommond();
				savevalidCommond.setUserid(userInfo.getUserId());		
				savevalidCommond.setUser(user);
				savevalidCommond.setUserName(userInfo.getUserName());
				this.save(savevalidCommond);
			}		
			//查询班级信息
			TrainingClass trainingClassValidCommond= trainingClassDao.find(TrainingClass.class, validCommond.getClassId());
			if(PropertyUtil.objectNotEmpty(trainingClassValidCommond)){		
				boolean checkNum=checkClassUserNum(trainingClassValidCommond,1);
				if(checkNum){	
				boolean flag=true;
				Date now=new Date();
//				DateUtil.
				Date beginDate=trainingClassValidCommond.getEnterBeginTime();
				Date endDate=trainingClassValidCommond.getEnterEndTime();
				if(beginDate.getTime()>now.getTime()){
					msg="报名还没开始";
					flag=false;
				}else if(now.getTime()>endDate.getTime()){
					msg="报名已截止";
					flag=false;
				}
				//在报名时间内
				if(flag){
					//验证用户是否进入过其他班级
					InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
					queryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());
					queryCommond.setSearchUserId(userInfo.getUserId());
					ClassUser clUser=interiorUserDao.checkUserInClass(queryCommond);
					if(PropertyUtil.objectNotEmpty(clUser)){
						//msg="您今年已参加过培训班，不能再次报名 ";
						msg=clUser.getUser().getLoginID()+"学员已经报名"+clUser.getTrainingClass().getClassMainName()+",您今年只能报名1个班。";
					}else{
						//查询分配名额
						String[] depIds=userInfo.getTreepath().split("/");						
						InteriorUserQueryCommond iuqueryCommond=new InteriorUserQueryCommond();
						//班级类型 普通班
						List<String> classTypeList=new ArrayList<String>();
						classTypeList.add(TrainingClass.PTB_CLASS_TYPE);
						DeptAssignedInfoQueryCommond deptAssignedInfoQueryCommond=new DeptAssignedInfoQueryCommond();
						deptAssignedInfoQueryCommond.setSearchDepIds(depIds);
						deptAssignedInfoQueryCommond.setSearchYear(Integer.parseInt(trainingClassValidCommond.getClassnameYear()));
						deptAssignedInfoQueryCommond.setSearchClassType(1);
						iuqueryCommond.setSearchClassTypeList(classTypeList);
						List<DeptAssignedInfoValidCommond> deptAssList=(List<DeptAssignedInfoValidCommond>)deptAssignedInfoService.findList(deptAssignedInfoQueryCommond);
						if(PropertyUtil.objectNotEmpty(deptAssList)&&deptAssList.size()>0){
							DeptAssignedInfoValidCommond deptAssInfo=deptAssList.get(0);				
							iuqueryCommond.setSeachDepId(deptAssInfo.getDepartment().getDepid());
							iuqueryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());
							List<DeptSignupBean> deptSidnupList=this.getDeptSignupNum(iuqueryCommond);
							if(PropertyUtil.objectNotEmpty(deptSidnupList)){
								DeptSignupBean deptSidnupbean=deptSidnupList.get(0);
								if(deptAssInfo.getRestrictPersonNum()>deptSidnupbean.getSignupNum().intValue()){
									msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.PERSONAL);
								}else{
									msg="单位年度报名已满 ";
								}
							}else{
								msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.PERSONAL);
							}
						}else{
							msg="人员所属单位没有参学名额";
						}							
					}		
				}				
			}else{
				msg="班级报名人数已满";
			}	
			}
		}else{
			msg="该用户不存在 ";
		}
		
		return msg;	
	}	
	
	/**
	 * 添加班级人员关系 默认审核通过
	 * @param validCommond
	 * @param user
	 * @return
	 * @author ShenPS
	 * @date 2015年1月9日
	 */
	public String saveClassUserIsAudY(TrainingClass trainingClass,User user,int signUpType){
		ClassUser classUser=new ClassUser();
//		TrainingClass trainingClass=new TrainingClass();
//		trainingClass.setClassId(training.getClassId());
		classUser.setTrainingClass(trainingClass);
		classUser.setCreateDate(new Date());
		classUser.setUser(user);
		classUser.setActiveState(1);
		classUser.setSignUpYear(trainingClass.getClassnameYear());
		classUser.setAuditingState(ClassUser.IS_AUDITING_Y);
		classUser.setAuditingUserDate(new Date());
		classUser.setSignUpType(signUpType);
		classUserDao.save(classUser);

		LogRecordParam param = new LogRecordParam();
		LogRecord logRecord = new LogRecord();
		param.setOpdataId(classUser.getClassUserId().toString());//操作主数据id
		logRecord.bulifLogParams(param);
		int beginIndex = param.getOpurl().lastIndexOf("/") + 1;
		int endIndex = param.getOpurl().indexOf(".");
		if (param.getOpurl().substring(beginIndex, endIndex).equals("addInteriorUser")) {
			param.setOpmodule("单位功能 - 单位报名");
			param.setOpcontent("单位报名");
		}
		if (param.getOpurl().substring(beginIndex, endIndex).equals("saveInteriorUser")) {
			param.setOpmodule("工委完善培训班 - 报名调整 - 新增");
			param.setOpcontent("工委报名");
		}
		param.setOpafteData("用户ID=====>" + user.getUserId() + "\n用户账户=====>" + user.getLoginID() + "\n用户姓名=====>" + user.getUserName() + 
				"\n班级ID=====>" + trainingClass.getClassId() + "\n班级名称=====>" + trainingClass.getClassMainName());//操作后数据
		param.setOptype(UniteConstants.ADD);
		try {
		   LogThread logThread = new LogThread(param);
			Thread thread = new Thread(logThread);
			thread.start();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "报名成功";
	}
	

	/**
	 * 
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年1月8日
	 */
	@Override
	public List<DeptSignupBean> getDeptSignupNum(InteriorUserQueryCommond queryCommond) throws Exception {
		if(PropertyUtil.objectNotEmpty(queryCommond.getSeachDepId())){
			ClassUserQueryCommond classUserQueryCommond=new ClassUserQueryCommond();
			classUserQueryCommond.setSearchDepId(queryCommond.getSeachDepId());
//			classUserQueryCommond.setSearchDepId(deptAssInfo.getDepartment().getDepid());
			List<Department> deptList=classUserDao.findDepTreePath(classUserQueryCommond);
			queryCommond.setSearchTreepath(deptList.get(0).getTreepath());	
		}
		return interiorUserDao.getDeptSignupNum(queryCommond);
	}
	
	
	public SignupBean checkUserIsInSignUP(InteriorUserQueryCommond queryCommond,UserInfoBean userInfo) throws Exception {
		SignupBean signBean=new SignupBean();
		boolean flag=false;
		ClassDeptQueryCommond cdQueryCommond=new ClassDeptQueryCommond();
		cdQueryCommond.setSearchClassId(queryCommond.getSearchClassId());
		List<ClassDeptQueryBean> classDepList=classDeptDao.getClassSeleDepList(cdQueryCommond);
		if(PropertyUtil.objectNotEmpty(classDepList)){
			String[] depIds=userInfo.getTreepath().split("/");
			for(ClassDeptQueryBean classDet:classDepList){
				for(int i=0;i<depIds.length;i++){
					if(depIds[i].equals(classDet.getDepId())){
						signBean.setClassDeptBean(classDet);
						flag=true;
						break;
					}
				}
			}
		}
		signBean.setFlag(flag);
		return signBean;
	}
	
	/**
	 * 检验班级人数
	 * @param trainingClass 班级信息
	 * @param signUpNum 报名人数
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年1月23日
	 */
	private boolean checkClassUserNum(TrainingClass trainingClass,Integer signUpNum) throws Exception {
		boolean flag=false;
		if(trainingClass.getClassUserNum()==-1){
			flag=true;
		}else{
			InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
			queryCommond.setSearchClassId(trainingClass.getClassId());
			//已报人数
			Long classSiupNum=interiorUserDao.getClassSingUpNum(queryCommond);
			//班级学员人数
			Integer classUserNum=trainingClass.getClassUserNum();
			//报名人数不为空			
			classSiupNum=classSiupNum+signUpNum;
			if(classUserNum>=classSiupNum.intValue()){
				flag=true;
			}
		}		
		return flag;
		
	}	
	

	/**
	 * 添加内部用户-单位报名
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2015-1-9
	 */
	public String addInteriorUser(InteriorUserValidCommond validCommond) throws Exception{
		String msg="";
		//调用dao层:根据部门id和登陆号查询此部门是否存在此用户
		List<UserInfoBean> users = preAuthorizationRegistrationDao.findBaseUser(validCommond.getLoginId(),validCommond.getDeptId());
		if(PropertyUtil.objectNotEmpty(users)&&users.size()>0){//存在 
			UserInfoBean userInfo=users.get(0);
			User user=new User();
			user.setUserId(userInfo.getUserId());
			user.setLoginID(userInfo.getLoginID());
			user.setUserName(userInfo.getUserName());
			//根据用户id查询内部用户表
			InteriorUserValidCommond updatevalidCommond=this.find(user.getUserId());
			if(PropertyUtil.objectNotEmpty(updatevalidCommond)){//有就更新姓名
				InteriorUserValidCommond vCommond=new InteriorUserValidCommond();
				vCommond.setUserid(userInfo.getUserId());
				vCommond.setUserName(user.getUserName());
				interiorUserDao.updateInteriorUser(vCommond);
				//updatevalidCommond.setUserName(user.getUserName());
			}else{//没有就新增
				InteriorUserValidCommond savevalidCommond=new InteriorUserValidCommond();
				savevalidCommond.setUserid(user.getUserId());		
				savevalidCommond.setUser(user);
				savevalidCommond.setUserName(user.getUserName());
				this.save(savevalidCommond);
			}
			//查询此用户是否已经进入过此班
			InteriorUserQueryCommond quCommond=new InteriorUserQueryCommond();
			quCommond.setSearchClassId(validCommond.getClassId());
			quCommond.setSearchUserId(user.getUserId());
			ClassUser cUser=interiorUserDao.checkUserInClass(quCommond);
			if(PropertyUtil.objectNotEmpty(cUser)){//报过本班就给出提示
				return "不能重复添加用户！";
			}
			//查询班级信息
			TrainingClass trainingClassValidCommond= trainingClassDao.find(TrainingClass.class, validCommond.getClassId());
			//查询此人此年是否报过其他班级
			InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
			if(PropertyUtil.objectNotEmpty(trainingClassValidCommond)){
				queryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());
				queryCommond.setSearchUserId(user.getUserId());
				ClassUser clUser=interiorUserDao.checkUserInClass(queryCommond);
				if(PropertyUtil.objectNotEmpty(clUser)){//报过其他班就给出提示
					//msg="该用户已经进入过其他班级！";
					msg=clUser.getUser().getLoginID()+"学员已经报名"+clUser.getTrainingClass().getClassMainName()+",该学员只能报名1个班级。";
				}else{//没报过其他班
					//检验班级人数是否已经报满
					boolean checkNum=checkClassUserNum(trainingClassValidCommond,1);
					if(checkNum){
						boolean flag=true;
						//验证此部门是否还能添加人员（此逻辑只限制班级类型为：国防班和定向班，即完善班级时指定部门的限制人数）
						if(!trainingClassValidCommond.getClassType().getDataCode().equals(TrainingClass.PTB_CLASS_TYPE)){
							//查询班级部门关系中此部门的限制人数
							ClassDeptQueryCommond classDeptQueryCommond=new ClassDeptQueryCommond();
							classDeptQueryCommond.setSearchClassId(validCommond.getClassId());
							classDeptQueryCommond.setSearchDepId(validCommond.getDeptId());
							List<ClassDeptValidCommond> classDeptList=(List<ClassDeptValidCommond>)classDeptService.findList(classDeptQueryCommond);
							if(PropertyUtil.objectNotEmpty(classDeptList)&&classDeptList.size()>0){//存在班级部门的限制人数
								//班级部门的限制人数
								int classDeptLimitNum=classDeptList.get(0).getLimitNum();
								//查询本单位人员在此班已报人数
								int reportNum=0;
								queryCommond.setSearchClassId(trainingClassValidCommond.getClassId());
								queryCommond.setSeachDepId(validCommond.getDeptId());	
								List<DeptSignupBean> deptSignBenList=this.getDeptSignupNum(queryCommond);
								if(PropertyUtil.objectNotEmpty(deptSignBenList)&&deptSignBenList.size()>0){
									DeptSignupBean deptSignupBean=deptSignBenList.get(0);
									reportNum=deptSignupBean.getSignupNum().intValue();
								}
								//如果本单位此年已报人数和本次要报名人数之和小于等于班级部门的限制人数就可以添加，否则不能添加
								if(!(reportNum<classDeptLimitNum)){
									flag=false;
									msg="单位报名人数已满 ！";
								}
							}else{
								flag=false;
								msg="该用户不在报名范围内 ！";
							}
						}	
						//验证此部门是否还能添加人员（分配参学中限制的人数）
						if(flag){
							InteriorUserQueryCommond iuqueryCommond=new InteriorUserQueryCommond();
							//班级类型
							List<String> classTypeList=new ArrayList<String>();
							//查询分配名额
							DeptAssignedInfoQueryCommond deptAssignedInfoQueryCommond=new DeptAssignedInfoQueryCommond();
							deptAssignedInfoQueryCommond.setSearchDepId(validCommond.getDeptId());
							deptAssignedInfoQueryCommond.setSearchYear(Integer.parseInt(trainingClassValidCommond.getClassnameYear()));
							if(trainingClassValidCommond.getClassType().getDataCode().equals(TrainingClass.GFB_CLASS_TYPE)){
								deptAssignedInfoQueryCommond.setSearchClassType(DeptAssignedInfo.DEFENSE_CLASS);//国防班
								classTypeList.add(TrainingClass.GFB_CLASS_TYPE);
							}else{//普通班
								deptAssignedInfoQueryCommond.setSearchClassType(DeptAssignedInfo.ORDINARY_CLASS);
								classTypeList.add(TrainingClass.PTB_CLASS_TYPE);
								classTypeList.add(TrainingClass.DXB_CLASS_TYPE);
							}	
							iuqueryCommond.setSearchClassTypeList(classTypeList);
							List<DeptAssignedInfoValidCommond> deptAssList=(List<DeptAssignedInfoValidCommond>)deptAssignedInfoService.findList(deptAssignedInfoQueryCommond);
							if(PropertyUtil.objectNotEmpty(deptAssList)&&deptAssList.size()>0){//存在分配参学名额
								DeptAssignedInfoValidCommond deptAssInfo=deptAssList.get(0);				
								iuqueryCommond.setSeachDepId(validCommond.getDeptId());
								iuqueryCommond.setSearchSigupYear(trainingClassValidCommond.getClassnameYear());
								List<DeptSignupBean> deptSidnupList=this.getDeptSignupNum(iuqueryCommond);
								if(PropertyUtil.objectNotEmpty(deptSidnupList)){
									DeptSignupBean deptSidnupbean=deptSidnupList.get(0);
									if(deptAssInfo.getRestrictPersonNum()>deptSidnupbean.getSignupNum().intValue()){
										msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.UNIT);
									}else{
										msg="单位年度报名已满 ！";
									}
								}else{
									msg=saveClassUserIsAudY(trainingClassValidCommond,user,ClassUser.UNIT);
								}
							}else{//不存在单位参学名额给出提示
								msg="此单位没有分配参学名额，请分配以后再报名！";
							}								
						}
					}else{
						msg="此班级报名人数已满！";
					}
				}
			}else{
				msg="此班级已不存在！";
			}
		}else{//不存在给出提示
			msg="不存在此用户,请重新添加！";
		}
		return msg;	
	}
	/**
	 * 添加内部用户- Excel导入单位报名
	 * @param validCommond
	 * @param sheet
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2015-1-11
	 */
	@Override
	public String importInteriorUser(InteriorUserValidCommond validCommond,
			ExcelSheet sheet) throws Exception {
		synchronized(o) {
			StringBuffer rmsg=new StringBuffer();
			StringBuffer bmsg=new StringBuffer();
			StringBuffer msg=new StringBuffer();
			List<ExcelRow> eRows=sheet.getRowList();
			//判断导入数据是否为空
			if(PropertyUtil.objectNotEmpty(eRows)&&eRows.size()>1){//不为空
				//判断excel的表头对不对
				boolean state=false;
				try {
					ExcelRow topRow=eRows.get(0);
					List<ExcelCell> topRowCells =topRow.getCellList();
					if(topRowCells.size()>2) {
						state=true;
					}
					if(topRowCells.get(0).getCellValue()==null||topRowCells.get(0).getCellValue().toString().trim().length()==0||!topRowCells.get(0).getCellValue().toString().trim().equals("培训班编号")) {
						state=true;
					}
					if(topRowCells.get(1).getCellValue()==null||topRowCells.get(1).getCellValue().toString().trim().length()==0||!topRowCells.get(1).getCellValue().toString().trim().equals("学员代码")) {
						state=true;
					}
					if(state) {
						rmsg.append("您使用导入模板的表头不正确，请确认后重新导入！");
						return rmsg.toString();
					}
				}catch(Exception e) {
					rmsg.append("您使用导入模板的表头不正确，请确认后重新导入！");
					return rmsg.toString();
				}				
				//遍历数据，检查文档格式并将班级和人员对应存放到map集合中
				//开始获取单元格的值
				Map<String,List<ClassUserValidCommond>> map=new HashMap<String,List<ClassUserValidCommond>>();//key:班级编号    valeu:班级人员集合
				Map<String,User> userMap=new HashMap<String,User>();//key:登陆号     value:用户
				Map<String,List<TrainingClassValidCommond>> trainingClassMap=new HashMap<String,List<TrainingClassValidCommond>>();//key:班级的年度    value：班级集合
				Map<String,List<TrainingClassValidCommond>> trainingClassMap2=new HashMap<String,List<TrainingClassValidCommond>>();//key:班级的班级类型    value：班级集合
				List<ClassUserValidCommond> allClassUserList=new ArrayList<ClassUserValidCommond>();//全部班级人员集合
				for(int i=1;i<eRows.size();i++){
					ClassUserValidCommond classUserValidCommond = new ClassUserValidCommond();
					List<ClassUserValidCommond> classUserList=new ArrayList<ClassUserValidCommond>();
					TrainingClass trainingClass = new TrainingClass();
					User user = new User();
					ExcelRow eRow=eRows.get(i);
					try {
						List<ExcelCell> eCells =eRow.getCellList();
						//判断是否是空行  如果都为空不做处理
						boolean nullNum=judgeNullRow(eCells,2);
						if(nullNum) {
							continue;
						}
						//第一列为班级编号
						if(eCells.get(0).getCellValue()==null||eCells.get(0).getCellValue().toString().trim().length()==0) {
							rmsg.append("第" + eRow.getRowNum() + "行 \"班级编号\" " + "列不能为空，请修改后重新导入。<br/>") ;	
						}
						if(eCells.get(0).getCellValue()!=null&&eCells.get(0).getCellValue().toString().trim().length()>11) {
							rmsg.append("第" + eRow.getRowNum() + "行 \"班级编号\" " + "列输入字数超过了8字，请修改后重新导入。<br/>") ;	
						}
						if(!(eCells.get(0).getCellValue()==null||eCells.get(0).getCellValue().toString().trim().length()==0)) {
							//如果班级编号是纯数字时传入后台为double类型，所以需要转化一下
							String sContentValue =eCells.get(0).getCellValue().toString();
							boolean bCheckResult = true;
							Double dCheckValue=0.0;
							try{   
								dCheckValue = Double.parseDouble(sContentValue); 
								if (dCheckValue instanceof Double == false){  
									bCheckResult = false;  
								}  
							}catch(NumberFormatException e){    
								bCheckResult = false;  
							}  
							if(bCheckResult){
								Integer classNum=dCheckValue.intValue();
								trainingClass.setClassNum(classNum.toString());
							}else{
								trainingClass.setClassNum(eCells.get(0).getCellValue().toString());
							}
						}
						//第二列为登录号
						if(eCells.get(1).getCellValue()==null||eCells.get(1).getCellValue().toString().trim().length()==0) {
							rmsg.append("第" + eRow.getRowNum() + "行 \"登陆号\" " + "列不能为空，请修改后重新导入。<br/>") ;	
						}
						if(!(eCells.get(1).getCellValue()==null||eCells.get(1).getCellValue().toString().trim().length()==0)) {
							//如果登录号是纯数字时传入后台为double类型，所以需要转化一下
							String sContentValue =eCells.get(1).getCellValue().toString();
							boolean bCheckResult = true;
							Double dCheckValue=0.0;
							try{   
								dCheckValue = Double.parseDouble(sContentValue); 
								if (dCheckValue instanceof Double == false){  
									bCheckResult = false;  
								}  
							}catch(NumberFormatException e){    
								bCheckResult = false;  
							}  
							if(bCheckResult){
								Integer loginId=dCheckValue.intValue();
								user.setLoginID(loginId.toString());
								if(eCells.get(1).getCellValue()!=null&&loginId.toString().trim().length()>15) {
									rmsg.append("第" + eRow.getRowNum() + "行 \"登陆号\" " + "列输入字数超过了15字，请修改后重新导入。<br/>") ;	
								}
							}else{
								user.setLoginID(eCells.get(1).getCellValue().toString());
								if(eCells.get(1).getCellValue()!=null&&eCells.get(1).getCellValue().toString().trim().length()>15) {
									rmsg.append("第" + eRow.getRowNum() + "行 \"登陆号\" " + "列输入字数超过了15字，请修改后重新导入。<br/>") ;	
								}
							}
						}
						classUserValidCommond.setTrainingClass(trainingClass);
						classUserValidCommond.setUser(user);
						allClassUserList.add(classUserValidCommond);
						if(!PropertyUtil.objectNotEmpty(map.get(trainingClass.getClassNum()))){
							classUserList.add(classUserValidCommond);
							map.put(trainingClass.getClassNum(), classUserList);
						}else{
							List<ClassUserValidCommond> culist=map.get(trainingClass.getClassNum());
							culist.add(classUserValidCommond);
							map.put(trainingClass.getClassNum(), culist);
						}	
						userMap.put(user.getLoginID(), user);
					}catch (Exception e){
						rmsg.append("第" + eRow.getRowNum() + "行数据不正确，请修改后重新导入。 <br/>");
					}
				}
				if(!rmsg.toString().equals("")){
					return rmsg.toString();
				}
				//获得所有的班级编号
				Set<String> classNums = map.keySet();
				if(PropertyUtil.objectNotEmpty(classNums)&&classNums.size()>0){
					//判断本次报名班级是否正确（需要判断班级是否存在，是否班级在报名时间内）
					for(String classNum:classNums){
						List<TrainingClassValidCommond> trainingClassList=new ArrayList<TrainingClassValidCommond>();
						List<TrainingClassValidCommond> trainingClassList2=new ArrayList<TrainingClassValidCommond>();
						TrainingClassQueryCommond trainingClassQueryCommond = new TrainingClassQueryCommond();
						trainingClassQueryCommond.setSearchClassNum(classNum);
						Date date=new Date();
						SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
						String time=sdf.format(date);
						trainingClassQueryCommond.setSearchEnterBeginTimeDate(sdf.parse(time));
						trainingClassQueryCommond.setSearchEnterEndTimeDate(sdf.parse(time));
						trainingClassQueryCommond.setSearchActiveState(TrainingClass.IS_ACTIVE_Y);//活动班级
						trainingClassQueryCommond.setSearchPublishState(TrainingClass.IS_PUBLISH_Y);//已发布班级
						trainingClassQueryCommond.setAuditState(TrainingClass.IS_AUDIT_Y);//审核通过班级
						List<TrainingClassValidCommond> trainingClass=(List<TrainingClassValidCommond>) trainingClassService.findAllList(trainingClassQueryCommond);
						if(!(PropertyUtil.objectNotEmpty(trainingClass)&&trainingClass.size()>0)){//报名班级不符合报名条件
							msg.append("班级编号为"+classNum+"的班级不存在或者不在报名时间内，请修改后重新导入。<br/>");
						}else{//符合报名条件就将班级存放到集合中
							//检验班级人数是否已经报满
							TrainingClassValidCommond tcv=trainingClass.get(0);
							//获得本次此班要报名人数
							int preReportNum=0;
							List<ClassUserValidCommond> cuList=map.get(tcv.getClassNum());
							if(PropertyUtil.objectNotEmpty(cuList)&&cuList.size()>0){
								preReportNum=cuList.size();
							} 
							TrainingClass tc=(TrainingClass)tcv.toEntity();
							boolean checkNum=checkClassUserNum(tc,preReportNum);
							if(checkNum){
								if(!PropertyUtil.objectNotEmpty(trainingClassMap.get(trainingClass.get(0).getClassnameYear()))){
									trainingClassList.add(trainingClass.get(0));
									trainingClassMap.put(trainingClass.get(0).getClassnameYear(),trainingClassList);	
								}else{
									List<TrainingClassValidCommond> tcList=trainingClassMap.get(trainingClass.get(0).getClassnameYear());
									tcList.add(trainingClass.get(0));
									trainingClassMap.put(trainingClass.get(0).getClassnameYear(),tcList);
								}
								if(!PropertyUtil.objectNotEmpty(trainingClassMap2.get(trainingClass.get(0).getClassType()))){
									trainingClassList2.add(trainingClass.get(0));
									trainingClassMap2.put(trainingClass.get(0).getClassType(),trainingClassList2);
								}else{
									List<TrainingClassValidCommond> tcvList=trainingClassMap2.get(trainingClass.get(0).getClassType());
									tcvList.add(trainingClass.get(0));
									trainingClassMap2.put(trainingClass.get(0).getClassType(),tcvList);
								}	
							}else{
								msg.append("班级编号为"+classNum+"的班级报名人数已满，请修改后重新导入。<br/>");
							}
						}
					}	
				}else{
					msg.append("报名班级编码不能为空，请修改后重新导入！");
				}	
				if(!msg.toString().equals("")) {
					return msg.toString();
				}
				//获得所有的登录号
				Set<String> loginIds = userMap.keySet();
				if(PropertyUtil.objectNotEmpty(loginIds)&&loginIds.size()>0){
					//遍历登录号
					for(String loginId:loginIds){
						//调用dao层:根据部门id和登陆号查询此部门是否存在此用户
						List<UserInfoBean> users = preAuthorizationRegistrationDao.findBaseUser(loginId,validCommond.getDeptId());
						if(PropertyUtil.objectNotEmpty(users)&&users.size()>0){//存在 
							UserInfoBean userInfo=users.get(0);
							User user=new User();
							user.setUserId(userInfo.getUserId());
							user.setLoginID(userInfo.getLoginID());
							user.setUserName(userInfo.getUserName());
							//根据用户id查询内部用户表
							InteriorUserValidCommond updatevalidCommond=this.find(user.getUserId());
							if(PropertyUtil.objectNotEmpty(updatevalidCommond)){//有就更新姓名
								InteriorUserValidCommond vCommond=new InteriorUserValidCommond();
								vCommond.setUserid(userInfo.getUserId());
								vCommond.setUserName(user.getUserName());
								interiorUserDao.updateInteriorUser(vCommond);
							//	updatevalidCommond.setUserName(user.getUserName());
							}else{//没有就新增
								InteriorUserValidCommond savevalidCommond=new InteriorUserValidCommond();
								savevalidCommond.setUserid(user.getUserId());		
								savevalidCommond.setUser(user);
								savevalidCommond.setUserName(user.getUserName());
								this.save(savevalidCommond);
							}
							//由登录号获得用户
							if(PropertyUtil.objectNotEmpty(userMap.get(user.getLoginID()))){
								User u=userMap.get(user.getLoginID());
								u.setUserId(user.getUserId());
							}
						}else{
							msg.append("登录号为"+loginId+"的用户不存在，请修改后重新导入。<br/>");
						}
					}
				}else{
					msg.append("登录号不能为空，请修改后重新导入。");
				}
				if(!msg.toString().equals("")) {
					return msg.toString();
				}
				//判断本次报名人员中是否有重复（即是否在同一个班有两条以上记录），是否在同一年有重复（即是否在同一年有两条以上的记录）
				//判断是否在同一个班有两条以上记录
				if(PropertyUtil.objectNotEmpty(classNums)&&classNums.size()>0){
					for(String classNum:classNums){
						//key:登录号  value:classNums班中的人员
						Map<String,List<ClassUserValidCommond>> cuMap=new HashMap<String,List<ClassUserValidCommond>>();
						//获得classNums班中的人员
						List<ClassUserValidCommond> cus=map.get(classNum);
						//同一个loginId中的人员集合
						List<ClassUserValidCommond> cusByLoginId=new ArrayList<ClassUserValidCommond>();
						for(ClassUserValidCommond cu:cus){
							if(!PropertyUtil.objectNotEmpty(cuMap.get(cu.getUser().getLoginID()))){
								cusByLoginId.add(cu);
								cuMap.put(cu.getUser().getLoginID(),cusByLoginId);
							}else{
								bmsg.append("登录号为"+cu.getUser().getLoginID()+"的用户不能同时出现在班级编号为"+classNum+"的班中，请修改后重新导入。<br/>");
							}	
						}
					}
				}
				//获得本次报名班级的所有年度
				Set<String> years = trainingClassMap.keySet();
				//判断是否在同一年有两条以上的记录
				if(PropertyUtil.objectNotEmpty(years)&&years.size()>0){
					for(String year:years){
						//key:登录号  value:classNums班中的人员
						Map<String,List<ClassUserValidCommond>> cuMap=new HashMap<String,List<ClassUserValidCommond>>();
						//由年--->班级--->班级人员--->判断是否有重复的人
						List<TrainingClassValidCommond> classList=trainingClassMap.get(year);
						for(TrainingClassValidCommond tcv:classList){
							//获得此班中的人员
							List<ClassUserValidCommond> cus=map.get(tcv.getClassNum());
							for(ClassUserValidCommond cu:cus){
								//同一个loginId中的人员集合
								List<ClassUserValidCommond> cusByLoginId=new ArrayList<ClassUserValidCommond>();
								if(!PropertyUtil.objectNotEmpty(cuMap.get(cu.getUser().getLoginID()))){
									cusByLoginId.add(cu);
									cuMap.put(cu.getUser().getLoginID(),cusByLoginId);
								}else{
									msg.append("登录号为"+cu.getUser().getLoginID()+"的用户同一年不能报名两次以上，请修改后重新导入。<br/>");
								}	
							}	
						}	
					}
				}
				if(!bmsg.toString().equals("")) {
					return bmsg.toString();
				}
				if(!msg.toString().equals("")){
					return msg.toString();
				}
				//遍历年份集合
				for(String year:years){
					//由年--->班级--->班级人员--->人
					List<String> userIds=new ArrayList<String>();
					Map<Integer,List<TrainingClassValidCommond>> deptAssignedInfoClassTypeMap=new HashMap<Integer,List<TrainingClassValidCommond>>();//key:单位年度参学名额中培训班类型     value：班级集合
					//获得year年的人员集合
					List<TrainingClassValidCommond> classList=trainingClassMap.get(year);
					for(TrainingClassValidCommond tcv:classList){
						List<ClassUserValidCommond> cuv=map.get(tcv.getClassNum());
						for(ClassUserValidCommond classUserValidCommond:cuv){
							User uu=userMap.get(classUserValidCommond.getUser().getLoginID());
							userIds.add(uu.getUserId());
						}	
						List<TrainingClassValidCommond> trainingClassList2=new ArrayList<TrainingClassValidCommond>();
						//获得tcv班级的班级类型
						String classType=tcv.getClassType();
						//获得单位年度参学名额对应的培训班类型     1:普通班（包括班级表中的普通班和定向班）      2：国防班
						if(classType.equals(TrainingClass.GFB_CLASS_TYPE)){//班级表中的班级类型为国防班时
							if(!PropertyUtil.objectNotEmpty(deptAssignedInfoClassTypeMap.get(DeptAssignedInfo.DEFENSE_CLASS))){
								trainingClassList2.add(tcv);
								deptAssignedInfoClassTypeMap.put(DeptAssignedInfo.DEFENSE_CLASS,trainingClassList2);
							}else{
								List<TrainingClassValidCommond> ctlist=deptAssignedInfoClassTypeMap.get(DeptAssignedInfo.DEFENSE_CLASS);
								ctlist.add(tcv);
								deptAssignedInfoClassTypeMap.put(DeptAssignedInfo.DEFENSE_CLASS,ctlist);
							}	
						}else{//班级表中的班级类型为普通班和定向班时
							if(!PropertyUtil.objectNotEmpty(deptAssignedInfoClassTypeMap.get(DeptAssignedInfo.ORDINARY_CLASS))){
								trainingClassList2.add(tcv);
								deptAssignedInfoClassTypeMap.put(DeptAssignedInfo.ORDINARY_CLASS,trainingClassList2);
							}else{
								List<TrainingClassValidCommond> ctlist=deptAssignedInfoClassTypeMap.get(DeptAssignedInfo.ORDINARY_CLASS);
								ctlist.add(tcv);
								deptAssignedInfoClassTypeMap.put(DeptAssignedInfo.ORDINARY_CLASS,ctlist);
							}	
						}	
					}
					if(PropertyUtil.objectNotEmpty(userIds)&&userIds.size()>0){
						//查询本次year年报名人员是否已经有人报过班
						InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
						queryCommond.setSearchSigupYear(year);
						queryCommond.setSearchUserIds(userIds);
						ClassUser clUser=interiorUserDao.checkUserInClass(queryCommond);
						if(PropertyUtil.objectNotEmpty(clUser)){//报过其他班就给出提示
							return "本次报名人员中存在已完成报名的人员，不能重复报名，请修改后重新导入。";
						}
					}
					//获得year年所有的单位年度参学名额中培训班类型
					Set<Integer> classTypes=deptAssignedInfoClassTypeMap.keySet();
					if(PropertyUtil.objectNotEmpty(classTypes)&&classTypes.size()>0){
						for(Integer classType:classTypes){
							List<String> classTypeList=new ArrayList<String>();
							String classTypeName="";
							//查询此单位year年classType培训班类型的分配参学限额数
							DeptAssignedInfoQueryCommond deptAssignedInfoQueryCommond=new DeptAssignedInfoQueryCommond();
							deptAssignedInfoQueryCommond.setSearchDepId(validCommond.getDeptId());
							deptAssignedInfoQueryCommond.setSearchYear(Integer.parseInt(year));
							if(classType.equals(DeptAssignedInfo.ORDINARY_CLASS)){//1:普通
								deptAssignedInfoQueryCommond.setSearchClassType(DeptAssignedInfo.ORDINARY_CLASS);
								classTypeName="普通班";
								classTypeList.add(TrainingClass.PTB_CLASS_TYPE);
								classTypeList.add(TrainingClass.DXB_CLASS_TYPE);
							}
							if(classType.equals(DeptAssignedInfo.DEFENSE_CLASS)){//2:国防
								deptAssignedInfoQueryCommond.setSearchClassType(DeptAssignedInfo.DEFENSE_CLASS);
								classTypeName="国防班";
								classTypeList.add(TrainingClass.GFB_CLASS_TYPE);
							}
							List<DeptAssignedInfoValidCommond> deptAssList=(List<DeptAssignedInfoValidCommond>)deptAssignedInfoService.findList(deptAssignedInfoQueryCommond);
							if(!(PropertyUtil.objectNotEmpty(deptAssList)&&deptAssList.size()>0)){//不存在分配参学名额给出提示
								bmsg.append( "此单位"+year+"年"+classTypeName+"没有分配参学名额，请分配以后再报名。<br/>");
							}else{//存在分配参学名额
								//获得此单位分配参学限额数
								DeptAssignedInfoValidCommond deptAssInfo=deptAssList.get(0);
								int restrictPersonNum=deptAssInfo.getRestrictPersonNum();
								//查询此单位year年在所有班的已报人数 （要对应班级表中的班级类型）
								int allReportNum=0;
								InteriorUserQueryCommond iuqueryCommond=new InteriorUserQueryCommond();
								iuqueryCommond.setSeachDepId(validCommond.getDeptId());
								iuqueryCommond.setSearchSigupYear(year);
								iuqueryCommond.setSearchClassTypeList(classTypeList);
								List<DeptSignupBean> deptSignBenList=this.getDeptSignupNum(iuqueryCommond);
								if(PropertyUtil.objectNotEmpty(deptSignBenList)&&deptSignBenList.size()>0){
									DeptSignupBean deptSignupBean=deptSignBenList.get(0);
									allReportNum=deptSignupBean.getSignupNum().intValue();
								}
								//获得本次year年classType培训班类型的要报名人数
								int allPreReportNum=0;
								//由year年classType培训班类型--->year年classType培训班类型的所有班--->year年classType培训班类型的所有班级中的人员
								List<TrainingClassValidCommond> classsList=deptAssignedInfoClassTypeMap.get(classType);
								if(PropertyUtil.objectNotEmpty(classsList)&&classsList.size()>0){
									for(TrainingClassValidCommond ttcv:classsList){
										List<ClassUserValidCommond> users=map.get(ttcv.getClassNum());
										allPreReportNum+=users.size();
									}
								}
								//如果此单位year年在所有班的已报人数和本次year年要报名人数之和小于等于此单位分配参学限额数就可以添加，否则不能添加
								if(!(allReportNum+allPreReportNum<=restrictPersonNum)){//不能添加给出提示
									msg.append("已经超过此单位"+year+"年"+classTypeName+"参学名额。<br/>");	
								}
							}	
						}	
					}	
				}	
				if(!bmsg.toString().equals("")) {
					return bmsg.toString();
				}
				if(!msg.toString().equals("")){
					return msg.toString();
				}
				//获得所有班级的所有班级类型
				Set<String> classTypeSet=trainingClassMap2.keySet();
				if(PropertyUtil.objectNotEmpty(classTypeSet)&&classTypeSet.size()>0){
					for(String classType:classTypeSet){
						//验证此部门是否还能添加人员到相应班级中（此逻辑只限制班级类型为：国防班和定向班，即完善班级时班级部门的限制人数）
						if(!classType.equals(TrainingClass.PTB_CLASS_TYPE)){
							//根据班级的班级类型获得班级集合
							List<TrainingClassValidCommond> tcvList=trainingClassMap2.get(classType);
							if(PropertyUtil.objectNotEmpty(tcvList)&&tcvList.size()>0){
								for(TrainingClassValidCommond tcv:tcvList){
									//查询班级部门关系中此部门的限制人数
									ClassDeptQueryCommond classDeptQueryCommond=new ClassDeptQueryCommond();
									classDeptQueryCommond.setSearchClassId(tcv.getClassId());
									classDeptQueryCommond.setSearchDepId(validCommond.getDeptId());
									List<ClassDeptValidCommond> classDeptList=(List<ClassDeptValidCommond>)classDeptService.findList(classDeptQueryCommond);
									if(PropertyUtil.objectNotEmpty(classDeptList)&&classDeptList.size()>0){//存在班级部门的限制人数
										//班级部门的限制人数
										int classDeptLimitNum=classDeptList.get(0).getLimitNum();
										//查询本单位人员在此班已报人数
										int reportNum=0;
										InteriorUserQueryCommond queryCommond=new InteriorUserQueryCommond();
										queryCommond.setSearchClassId(tcv.getClassId());
										queryCommond.setSeachDepId(validCommond.getDeptId());	
										queryCommond.setSearchSigupYear(tcv.getClassnameYear());
										List<DeptSignupBean> deptSignBenList=this.getDeptSignupNum(queryCommond);
										if(PropertyUtil.objectNotEmpty(deptSignBenList)&&deptSignBenList.size()>0){
											DeptSignupBean deptSignupBean=deptSignBenList.get(0);
											reportNum=deptSignupBean.getSignupNum().intValue();
										}
										//获得本次此班要报名人数
										int preReportNum=0;
										List<ClassUserValidCommond> cuList=map.get(tcv.getClassNum());
										if(PropertyUtil.objectNotEmpty(cuList)&&cuList.size()>0){
											preReportNum=cuList.size();
										} 
										//如果本单位人员在此班已报人数和本次此班要报名人数之和小于等于班级部门的限制人数就可以添加，否则不能添加
										if(!(reportNum+preReportNum<=classDeptLimitNum)){//不能添加给出提示
											msg.append("已超过此单位在班级编号为"+tcv.getClassNum()+"的班级中报名人数的限制。<br/>");
										}
									}else{//不存在班级部门的限制人数给出提示
										bmsg.append( "此单位不在班级编号为"+tcv.getClassNum()+"的报名范围内 。<br/>");
									}
								}
							}
						}	
					}	
				}
				if(!bmsg.toString().equals("")) {
					return bmsg.toString();
				}
				if(!msg.toString().equals("")){
					return msg.toString();
				}
				//如果以上条件全部符合就执行添加操作
				if(PropertyUtil.objectNotEmpty(allClassUserList)&&allClassUserList.size()>0){
					for(ClassUserValidCommond cu:allClassUserList){
						//由班级编号查询班级
						TrainingClassQueryCommond trainingClassQueryCommond = new TrainingClassQueryCommond();
						trainingClassQueryCommond.setSearchClassNum(cu.getTrainingClass().getClassNum());
						List<TrainingClassValidCommond> trainingClassList=(List<TrainingClassValidCommond>) trainingClassService.findAllList(trainingClassQueryCommond);
						//由登录号查询用户
						User user=interiorUserDao.getUserByLoginId(cu.getUser().getLoginID());
						if(PropertyUtil.objectNotEmpty(trainingClassList)&&trainingClassList.size()>0){
							TrainingClassValidCommond trainingClass=trainingClassList.get(0);
							//补全cu的其余属性
							cu.setCreateDate(new Date());
							cu.setActiveState(ClassUser.IS_AUDITING_Y);
							cu.setSignUpYear(trainingClass.getClassnameYear());
							cu.setAuditingState(ClassUser.IS_AUDITING_Y);
							cu.setAuditingUserDate(new Date());
							cu.setTrainingClass(trainingClass.getTrainingClass());
							cu.setUser(user);
							cu.setSignUpType(ClassUser.UNIT);
							this.save(cu);

							LogRecordParam param = new LogRecordParam();
							LogRecord logRecord = new LogRecord();
							param.setOpdataId(cu.getClassUserId().toString());//操作主数据id
							param.setOpafteData("用户ID=====>" + user.getUserId() + "\n用户账户=====>" + user.getLoginID() + "\n用户姓名=====>" + user.getUserName() + 
									"\n班级ID=====>" + trainingClass.getClassId() + "\n班级名称=====>" + trainingClass.getClassMainName());//操作后数据
							param.setOpmodule("单位功能 - 单位批量报名");
							param.setOpcontent("单位批量报名");
							param.setOptype(UniteConstants.ADD); 
							logRecord.bulifLogParams(param);
							try {
							   LogThread logThread = new LogThread(param);
								Thread thread = new Thread(logThread);
								thread.start();
							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							}
						}
					}
				}
			}else{//为空给出提示
				return "您导入的数据为空，请确认后重新导入！";
			}
			return "";
		}
	}
	/*
	 * 判断这一行是否为空
	 */
	private boolean judgeNullRow(List<ExcelCell> eCells,int column) {
		int sumNum=0;
		if(eCells.size()<column) {
			column=eCells.size();
		}
		for(int i=0;i<column;i++) {
			if(eCells.get(i).getCellValue()==null||eCells.get(i).getCellValue().toString().trim().length()==0) {
				sumNum=sumNum+1;
			}
		}
		
		
		if(sumNum==column) {
			return true;
		}
		
		
		return false;
	}
	
	/**
	 * 临时人员列表-单位and工委
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2015-5-6
	 */
	@Override
	public List<QueryClassUserBean> getLUserList(
			InteriorUserQueryCommond queryCommond) throws Exception {
		//工委管理员才会有此逻辑，工委下临时部门有两个
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchDepIds())){
			//查询部门treepath
			String treepath1="";
			String treepath2="";
			for(int i=0;i<queryCommond.getSearchDepIds().size();i++){
				ClassUserQueryCommond qCommond = new ClassUserQueryCommond();
				qCommond.setSearchDepId(queryCommond.getSearchDepIds().get(i));
				List<Department> department=classUserDao.findDepTreePath(qCommond);
				if(PropertyUtil.objectNotEmpty(department)&&department.size()>0){
					if(0==i){
						treepath1=department.get(0).getTreepath();
					}else if(1==i){
						treepath2=department.get(0).getTreepath();
					}
				}
			}
			queryCommond.setSearchTreePath1(treepath1);
			queryCommond.setSearchTreePath2(treepath2);	
		}
		//return interiorUserDao.getLUserList(queryCommond);
		return this.getList(interiorUserDao.getLUserList(queryCommond));
	}
	/**
	 * 修改用户
	 * @param vCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年5月6日
	 */
	public void updateUser(InteriorUserValidCommond vCommond)
			throws Exception {
		//修改用户名称时进行加密操作 by Max at 2015-08-31
		String name=new String(base64Util.encryptBASE64(vCommond.getUserName()));
		vCommond.setUserName(name);
		interiorUserDao.updateBaseUser(vCommond);
		interiorUserDao.updateInteriorUser(vCommond);
	}
	/**
	 * 预更新用户
	 * @param qCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年5月6日
	 */
	public InteriorUserQueryBean preUpdateUser(
			InteriorUserQueryCommond qCommond) throws Exception {
		return interiorUserDao.preUpdateUser(qCommond);
	}
	
	
	/**
	 * 修改内部用户加密 只使用一次
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年5月5日
	 */
	public int encryptionInteriorUser(InteriorUserQueryCommond queryCommond)
			throws Exception {
		
		List<InteriorUserValidCommond> validCommond=(List<InteriorUserValidCommond>)this.findAllList(queryCommond);
		for(InteriorUserValidCommond relut:validCommond){
			relut.setUserName(Base64Util.encryptBASE64(relut.getUserName()));
		}
		return 0;
	}
	/**
	 * 修改加密操作
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author ShenPS
	 * @date 2015年5月5日
	 */
	public int encryptionBaseUser()
			throws Exception {
		UserQueryCommond queryCommond=new UserQueryCommond();
		List<UserValidCommond> validCommond=(List<UserValidCommond>)userService.findAllList(queryCommond);		
		for(UserValidCommond relut:validCommond){
			relut.setUserName(Base64Util.encryptBASE64(relut.getUserName()));
		}
		return 0;
	}
	/**
	 * 解密方法
	 * @param list
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author GuoL
	 * @date 2015-5-5
	 */
	public List<QueryClassUserBean> getList(List<QueryClassUserBean> list) throws Exception{
		int i=0;
		for(;i<list.size();i++){
			if(list.get(i).getUserName()!=null&&list.get(i).getUserName()!=""){
				byte[] byteArray = Base64Util.decryptBASE64(list.get(i).getUserName());
				String name=new String(byteArray,"utf-8");
				list.get(i).setUserName(name);
			}
		}
		return list;
	}

	
}
